<?php

namespace app\stores\model\cash;

use app\common\model\CommonModel;


class ThirdSettlementModel extends CommonModel{

    protected  $name = 'beautiful_third_settlement';

    protected  $pk   = 'third_settlement_id';
    /**
     * 模型写入前事件（钩子函数）
     */
    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("edit_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }
    public static function info($where,$field='*')
    {
        $re = self::where($where)->field($field)->find();
        return $re;
    }
    public static function list($where,$order,$page=0,$limit=0,$field='*'){
        $query =  self::alias('ts')
            ->leftjoin('account a','a.account_id=ts.account_id')
            ->where(['ts.is_delete'=>0,'a.is_delete'=>0])
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where(['ts.status'=>$where['status']]);
            })
            ->when(isset($where['search']),function ($query) use ($where){
                $query->where('ts.settlement_name','like','%'.$where['search'].'%');
            })
            ->when(isset($where['shop_id']),function ($query) use ($where){
                $query->where(['ts.shop_id'=>$where['shop_id']]);
            })
            ->when(isset($where['settlement_type']),function ($query) use ($where){
                $query->where(['ts.settlement_type'=>$where['settlement_type']]);
            })->field($field);
        $count = $query->count();
        if($page>0) $query = $query->page($page);
        if($limit>0) $query = $query->limit($limit);
        $list = $query->order($order)->select()->toArray();

        return compact('count','list');
    }

}

